﻿using CLAP;
using SoldiHive.Core;
using SoldiHive.Infrustructure;
using System;
using System.Globalization;
using System.Linq;

namespace SoldiHive.Console
{
	public class Process
	{
		private IMarketViewer marketViewer;
		private IPrinter printer;

		public Process(IMarketViewer marketViewer, IPrinter printer)
		{
			this.marketViewer = marketViewer;
			this.printer = printer;
		}

		[Verb(Aliases = "calendar")]
		public void PrintCalendar(string from, string to)
		{
			DateTime? start = Process.ParseDate(from);
			DateTime? end = Process.ParseDate(to);
			if (!start.HasValue || !end.HasValue)
			{
				return;
			}

			var calendarViews = this.marketViewer.GetCalendarViews(start.Value, end.Value);
			foreach (var calendarView in calendarViews)
			{
				this.printer.PrintCalendarView(calendarView);
			}
		}

		[Verb(Aliases = "response")]
		public void PrintResponse(string tickers)
		{
			var tickersList = tickers.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
			foreach (var ticker in tickersList)
			{
				var responseViews = this.marketViewer.GetResponseViews(ticker);
				var company = responseViews.First().PastEarningsStatement.Company;
				System.Console.WriteLine("{0} {1}, {2}, {3}", company, company.Sector.Name, company.Industry.Name, company.SubIndustry.Name);
				foreach (var responseView in responseViews)
				{
					this.printer.PrintResponseView(responseView);
				}

				System.Console.WriteLine();
			}
		}

		[Verb(Aliases = "sector")]
		public void PrintSector(string ticker, string from, string to)
		{
			DateTime? start = Process.ParseDate(from);
			DateTime? end = Process.ParseDate(to);
			if (!start.HasValue || !end.HasValue)
			{
				return;
			}

			var sectorViews = this.marketViewer.GetSectorViews(ticker, start.Value, end.Value);
			foreach (var sectorView in sectorViews)
			{
				this.printer.PrintSectorView(sectorView);
			}
		}

		[Verb(Aliases="schedule")]
		public void PrintSchedule(string tickers)
		{
			var tickersList = tickers.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
			foreach (var ticker in tickersList)
			{
				var scheduleViews = this.marketViewer.GetScheduleView(ticker);

				this.printer.PrintResponseView(scheduleViews);
			}
		}

		[Verb(Aliases = "ratings")]
		public void PrintRatings(string tickers)
		{
			var tickersList = tickers.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
			foreach (var ticker in tickersList)
			{
				var ratingsView = this.marketViewer.GetRatingsView(ticker);

				this.printer.PrintRatingsView(ratingsView);
			}
		}

		private static DateTime? ParseDate(string value)
		{
			var dateFormat = "d/M/yyyy";
			var culture = CultureInfo.InvariantCulture;
			var styles = DateTimeStyles.None;

			DateTime? result = null;
			DateTime date;
			if (DateTime.TryParseExact(value, dateFormat, culture, styles, out date))
			{
				result = date; 
			}
			else
			{
				System.Console.WriteLine("Error parsing from date: {0}", date);
			}

			return result;
		}
	}
}
